<?php

/**
 * Controller_Review
 * 复习计划
 */

class Controller_review extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

    }

	/** 复习计划列表 **/
    public function actionReviewList ()
    {	
		
		// 全部年级
		$grade_list		= $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

    	// 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage`");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){	
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }
		// 4=复习计划
        $manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE `manage_type`='4' ORDER BY manage_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach($manage_list as $key=>$value){
			
			// 年级
			$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id={$value['grade_id']}");
			$manage_list[$key]['grade_name'] = $grade_data['grade_name'];
			// 学科
			$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id={$value['subject_id']}");
			$manage_list[$key]['subject_name'] = $subject_data['subject_name'];
			// 提交者
			$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
			$manage_list[$key]['staff_name'] = $staff_data['staff_name'];

		}
		// 搜索
		if($_POST){

			$grade_id     			= $this->post->grade_id;
			$subject_id     		= $this->post->subject_id;
			$start_time     		= $this->post->start_time;
			$end_time     			= $this->post->end_time;
			// 必须满足学科和年级同时搜索
			if(!empty($grade_id)&&empty($subject_id)){
			
				Helper_Client::alert("选择学科");
			
			}
			if(empty($grade_id)&& !empty($subject_id)){
			
				Helper_Client::alert("选择年级");
			
			}
			// 必须满足开始时间和借宿时间同时搜索
			if(!empty($start_time)&&empty($end_time)){
			
				Helper_Client::alert("选择截止时间");
			
			}
			if(empty($grade_id)&& !empty($end_time)){
			
				Helper_Client::alert("选择开始时间");
			
			}
			// 年级、学科搜索
			if(!empty($grade_id) &&!empty($subject_id)){
				
				$manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE grade_id={$grade_id} AND subject_id = {$subject_id} AND `manage_type`='4'");
				foreach($manage_list as $key=>$value){
			
					// 年级
					$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id={$value['grade_id']}");
					$manage_list[$key]['grade_name'] = $grade_data['grade_name'];
					// 学科
					$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id={$value['subject_id']}");
					$manage_list[$key]['subject_name'] = $subject_data['subject_name'];
					// 提交者
					$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
					$manage_list[$key]['staff_name'] = $staff_data['staff_name'];

				}
				
			}
			
			// 时间段搜索
			if(!empty($start_time) && !empty($end_time) ){
				
				$manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE `manage_type`='4' AND create_time>='{$start_time}' AND create_time<='{$end_time}'");
				foreach($manage_list as $key=>$value){
			
					// 年级
					$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id={$value['grade_id']}");
					$manage_list[$key]['grade_name'] = $grade_data['grade_name'];
					// 学科
					$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id={$value['subject_id']}");
					$manage_list[$key]['subject_name'] = $subject_data['subject_name'];
					// 提交者
					$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
					$manage_list[$key]['staff_name'] = $staff_data['staff_name'];

				}
			}

		}

        $page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
       
	    $this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("manage_list", $manage_list);
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("subject_list", $subject_list);

        $this->tpl->display('review/list.tpl.php');
    }
    /** 复习添加 **/
    public function actionReviewAdd()
    {        
		// 全部年级
		$grade_list		= $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 提交者未写

        if ($_POST){ 
			
			$grade_id     			= $this->post->grade_id;
			$subject_id     		= $this->post->subject_id;
            $manage_title     	    = $this->post->manage_title;
                       

            // 写入数据库
            $info = array( 

			"grade_id"       		=> $grade_id,
			"subject_id"       		=> $subject_id,
			"manage_title"  	    => $manage_title,            
			// 复习计划 =4
			"manage_type"       	=> 4,	
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_insert("tbl_manage", $info);
          Helper_Client::location("添加成功", "index.php?a=review.reviewlist");

         }

         $this->tpl->assign("grade_list", $grade_list);
		 $this->tpl->assign("subject_list", $subject_list); 
         $this->tpl->display("review/add.tpl.php");
    }
    /** 复习修改 **/
    public function actionReviewUpdate()
    {   

        $manage_id		= (int)$this->get->manage_id;
		// 标题
        $manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}' ");
		// 全部年级
		$grade_list		= $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

	    if (empty($manage_data)){
            Helper_Client::alert("复习计划不存在");
        }

        if ($_POST){ 

            $grade_id     			= $this->post->grade_id;
			$subject_id     		= $this->post->subject_id;
            $manage_title     	    = $this->post->manage_title;

            // 写入数据库
            $info = array(
				
			"grade_id"       		=> $grade_id,
			"subject_id"       		=> $subject_id,
			"manage_title"  	    => $manage_title,
			// 复习计划 =4
			"manage_type"       	=> 4,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");

          // 跳转
          Helper_Client::location("修改成功", "index.php?a=review.reviewlist");

         }

            $this->tpl->assign("manage_data", $manage_data);
			$this->tpl->assign("subject_list", $subject_list);
			$this->tpl->assign("grade_list", $grade_list);
	        $this->tpl->display("review/update.tpl.php");
    } 
	/** 复习计划删除 **/
    public function actionReviewDel()
    {
        $manage_id = $this->get->manage_id;   
        $this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=review.reviewlist");
    }

}

?>